﻿using System;
using System.Collections.Generic;
using System.Linq;
using SharpErp.Data.Common.Entity;
using SharpErp.Data.Common.View;

namespace SharpErp.Data.Service
{
    public partial class SharpErpServiceImpl : ISharpErpService
    {
        public List<CustomerThirdClass> GetSysCustomerThirdClass()
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.SelectAll(context.CustomerThirdClasEntities, p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
            }
        }

        public List<CustomerThirdClass> GetCustomerThirdClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return new List<CustomerThirdClass>(context.SelectAll(context.CustomerThirdClasEntities,
                    p => p.ParentId == parentId && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
            }
        }

        public List<CustomerThirdClass> GetCustomerThirdClassByParentIds(HashSet<int> ids)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return new List<CustomerThirdClass>(context.SelectAll(context.CustomerThirdClasEntities,
                    p => ids != null && ids.Contains(p.ParentId) && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
            }
        }

        public void AddCustomerThirdClass(CustomerThirdClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Add(context.CustomerThirdClasEntities, item);
            }
        }

        public void UpdateCustomerThirdClass(CustomerThirdClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Update(context.CustomerThirdClasEntities, item);
            }
        }

        public void DeleteCustomerThirdClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.CustomerThirdClasEntities, p => p.Id == id);
            }
        }

        public List<CustomerSecondClass> GetSysCustomerSecondClass()
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return  context.SelectAll(context.CustomerSencodClasEntities, p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
            }
        }

        public List<CustomerSecondClass> GetCustomerSecondClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return new List<CustomerSecondClass>(context.SelectAll(context.CustomerSencodClasEntities,
                    p => p.ParentId == parentId && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
            }

        }

        public List<CustomerSecondClass> GetCustomerSecondClassByParentIds(List<int> ids)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return new List<CustomerSecondClass>(context.SelectAll(context.CustomerSencodClasEntities,
                    p => ids.Contains(p.ParentId) && (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
            }
        }



        public int AddCustomerSecondClass(CustomerSecondClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return context.Add(context.CustomerSencodClasEntities, item).Id;
            }
        }

        public void UpdateCustomerSecondClass(CustomerSecondClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Update(context.CustomerSencodClasEntities, item);
            }
        }

        public void DeleteCustomerSecondClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.CustomerSencodClasEntities, p => p.Id == id);
            }
        }

        public string GetCustomerSecondClassNextCode()
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var maxId = context.CustomerSencodClasEntities.Max(p => p.Code);
                if (string.IsNullOrEmpty(maxId))
                {
                    return "SJS000001";
                }
                else
                {
                    return "SJS" + string.Format("{0:D6}", int.Parse(maxId.Substring(3)) + 1);
                }
            }
        }



        public List<CustomerFirstClass> GetSysCustomerFirstClass()
        {
            var list = GetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(KVEntity.KEY_CUSTOMER_FIRST_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
        }

        public void AddCustomerFirstClass(CustomerFirstClass item)
        {
            var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(KVEntity.KEY_CUSTOMER_FIRST_CLASS);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, KVEntity.KEY_CUSTOMER_FIRST_CLASS);
        }

        public void UpdateCustomerFirstClass(CustomerFirstClass item)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(KVEntity.KEY_CUSTOMER_FIRST_CLASS);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    break;
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(items, KVEntity.KEY_CUSTOMER_FIRST_CLASS);
            }
        }

        public void DeleteCustomerFirstClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(KVEntity.KEY_CUSTOMER_FIRST_CLASS);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.CustomerFirstClass>(items, KVEntity.KEY_CUSTOMER_FIRST_CLASS);
            }
        }
        public void AddCustomerGoodsClass(CustomerGoodsClass item)
        {

            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Add(context.CustomerGoodsClasses, item);
                if (item.CustomerFirstId < 3)
                {
                    var itemNineClassObject = context.SelectOne(context.ItemNineClasses, p => p.Id == item.NineClassId);
                    if (itemNineClassObject != null)
                    {
                        var itemEightClassObject = context.SelectOne(context.ItemEightClasses, p => p.Id == itemNineClassObject.ParentId);
                        var customerSecondObject = context.SelectOne(context.CustomerSencodClasEntities, p => p.Id == item.CustomerSecondId);
                        if (itemEightClassObject != null)
                        {
                            string manufactors = itemEightClassObject.Manufactor;
                            if (string.IsNullOrEmpty(manufactors))
                            {
                                itemEightClassObject.Manufactor = customerSecondObject?.Name;
                                context.Update(context.ItemEightClasses, itemEightClassObject);
                            }
                            else
                            {
                                var list = new List<string>(manufactors.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                                if (!list.Contains(customerSecondObject?.Name))
                                {
                                    itemEightClassObject.Manufactor += "," + (customerSecondObject?.Name);
                                    context.Update(context.ItemEightClasses, itemEightClassObject);
                                }
                            }
                        }
                    }
                }
                else
                {
                    var itemNineClassObject = context.SelectOne(context.MaterialNineClasses, p => p.Id == item.NineClassId);
                    if (itemNineClassObject != null)
                    {
                        var itemEightClassObject = context.SelectOne(context.MaterialEightClasses, p => p.Id == itemNineClassObject.ParentId);
                        var customerSecondObject = context.SelectOne(context.CustomerSencodClasEntities, p => p.Id == item.CustomerSecondId);
                        if (itemEightClassObject != null)
                        {
                            string manufactors = itemEightClassObject.Manufactor;
                            if (string.IsNullOrEmpty(manufactors))
                            {
                                itemEightClassObject.Manufactor = customerSecondObject?.Name;
                                context.Update(context.MaterialEightClasses, itemEightClassObject);
                            }
                            else
                            {
                                var list = new List<string>(manufactors.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                                if (!list.Contains(customerSecondObject?.Name))
                                {
                                    itemEightClassObject.Manufactor += "," + (customerSecondObject?.Name);
                                    context.Update(context.MaterialEightClasses, itemEightClassObject);
                                }
                            }
                        }
                    }

                }
            }
        }

        public void DeleteCustomerGoodsClass(int id)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.CustomerGoodsClasses, p => p.Id == id);
            }
        }

        public void DeleteCustomerGoodsClassByCustomerAndItem(int customerSecondId, int itemNineClassId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                context.Delete(context.CustomerGoodsClasses, p => p.CustomerSecondId == customerSecondId && p.NineClassId == itemNineClassId);
                var customerSecondObject = context.SelectOne(context.CustomerSencodClasEntities, p => p.Id == customerSecondId);
                if (customerSecondObject != null)
                {
                    if (customerSecondObject.ParentId < 3)
                    {
                        var itemNineClassObject = context.SelectOne(context.ItemNineClasses, p => p.Id == itemNineClassId);
                        var itemEightClassObject = context.SelectOne(context.ItemEightClasses, p => p.Id == itemNineClassObject.ParentId);
                        if (itemEightClassObject != null && !string.IsNullOrEmpty(itemEightClassObject.Manufactor))
                        {
                            var list = new List<string>(itemEightClassObject.Manufactor.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                            if (list.Contains(customerSecondObject?.Name))
                            {
                                list.Remove(customerSecondObject?.Name);
                                if (list.Count > 0)
                                {
                                    itemEightClassObject.Manufactor = list[0];
                                    for (int i = 1; i < list.Count; ++i)
                                    {
                                        itemEightClassObject.Manufactor += ("," + list[i]);
                                    }
                                }
                                else
                                {
                                    itemEightClassObject.Manufactor = "";
                                }
                                context.Update(context.ItemEightClasses, itemEightClassObject);
                            }
                        }
                    }
                    else
                    {
                        var itemNineClassObject = context.SelectOne(context.MaterialNineClasses, p => p.Id == itemNineClassId);
                        var itemEightClassObject = context.SelectOne(context.MaterialEightClasses, p => p.Id == itemNineClassObject.ParentId);
                        if (itemEightClassObject != null && !string.IsNullOrEmpty(itemEightClassObject.Manufactor))
                        {
                            var list = new List<string>(itemEightClassObject.Manufactor.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                            if (list.Contains(customerSecondObject?.Name))
                            {
                                list.Remove(customerSecondObject?.Name);
                                if (list.Count > 0)
                                {
                                    itemEightClassObject.Manufactor = list[0];
                                    for (int i = 1; i < list.Count; ++i)
                                    {
                                        itemEightClassObject.Manufactor += ("," + list[i]);
                                    }
                                }
                                else
                                {
                                    itemEightClassObject.Manufactor = "";
                                }
                                context.Update(context.MaterialEightClasses, itemEightClassObject);
                            }
                        }
                    }

                }
            }
        }

        public List<CustomerGoodsClass> GetCustomerGoods(int customerFirstId, int customerSecondId)
        {
            using (var context = new Common.Context.SharpErpDbContext())
            {
                return new System.Collections.Generic.List<CustomerGoodsClass>(context.SelectAll(context.CustomerGoodsClasses, p => p.CustomerSecondId == customerSecondId && p.CustomerSecondId == customerSecondId &&
                (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0));
            }
        }

        public List<QueryGoodsInfoView> GetCustomerGoodsView(int customerFirstId, int customerSecondId)
        {
            var result = new List<QueryGoodsInfoView>();
            var list = GetCustomerGoods(customerFirstId, customerSecondId);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    if (customerFirstId < 3 || customerFirstId > Common.Business.CustomerSecondClassConverter.START_PARENT_ID)
                    {
                        var newItem = getQueryGoodsInfoView(item.NineClassId);
                        if (newItem != null) result.Add(newItem);
                    }
                    else
                    {
                        var newItem = getQueryMaterialInfoView(item.NineClassId);
                        if(newItem != null) result.Add(newItem);
                    }

                }
            }
            return result;
        }
    }
}
